package com.crm.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.crm.entity.Netfollows;
import com.crm.entity.Students;
import com.crm.service.NetfollowsService;
import com.crm.service.StudentsService;
import com.crm.utils.DateUtil;
import com.crm.utils.DownloadUtil;
import com.crm.utils.ExcelUtil;
import com.crm.utils.FileUtil;
import com.crm.utils.ResponeMessageUtil;
import com.crm.utils.UUIDUtil;


@RestController
@RequestMapping("/excel")
public class ExcelController {
    
    @Autowired 
    private StudentsService studentsService;
    @Autowired 
    private NetfollowsService netfollowsService;
	/**
	 * 导出Excel数据
	 * 全部学生
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/studentsExport" , method = RequestMethod.GET, produces = "application/json")
	public void export(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos = null;
		OutputStream out = null;
		try {
			List<Students> students = studentsService.selectAll(null, null, null, null, null, null, null, null, null);
			
			baos = ExcelUtil.exportExcel(students, Students.class, "学生列表.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "学生列表.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	
	/**
	 * 导出Excel数据
	 * 我的学生
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportwdxs/{askerid}" , method = RequestMethod.GET, produces = "application/json")
	public void exportwdxs(HttpServletResponse response,@PathVariable("askerid") String askerid) throws Exception{
		ByteArrayOutputStream baos = null;
		OutputStream out = null;
		try {
			List<Students> students = studentsService.selectAll(null, null, null, askerid, null, null, null, null, null);
			
			baos = ExcelUtil.exportExcel(students, Students.class, "学生列表.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "学生列表.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	
	/**
	 * 导出Excel数据
	 * 跟踪学生
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportwlgz" , method = RequestMethod.GET, produces = "application/json")
	public void exportwdxs(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos = null;
		OutputStream out = null;
		try {
			List<Netfollows>  netfollows = netfollowsService.selectAll(null, null, null, null, null, null, null);
			baos = ExcelUtil.exportExcel(netfollows, Netfollows.class, "跟踪学生列表.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "跟踪学生列表.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	
	/**
	 * 导入Excel数据
	 * @param file
	 */
	@RequestMapping(value = "/import", method = RequestMethod.POST)
	public ResponeMessageUtil importExcel(MultipartFile file,String netpusherid,String createuser) {
		File f = null;
		try {
			// MultipartFile 转  file
			f = FileUtil.multipartFileToFile(file);
			List<Students> list = ExcelUtil.importExcel(f, Students.class, 2);
			for (Students data : list) {
				data.setId(UUIDUtil.getUUID());
				// 实际这里是保存数据库操作
				data.setCreatetime(DateUtil.dateToString(new Date()));
				data.setNetpusherid(netpusherid);
				data.setCreateuser(createuser);
				studentsService.insert(data);
			}
			return ResponeMessageUtil.ok("导入成功", null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (f != null) {
				f.delete();
			}
		}
		return ResponeMessageUtil.ok("导入失败", null);
	}
}
